package cn.edu.scau.cmi.chenjiale.factory.second;

import cn.edu.scau.cmi.chenjiale.factory.type.Insurance;
import cn.edu.scau.cmi.chenjiale.factory.type.InsuranceType;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * 主启动类
 * 
 * 
 * @author chen
 *
 */
public class SecondInsuranceApplication extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		// 主面板
		BorderPane mainPane = new BorderPane();
		// 个人面板
		BorderPane personPane = new BorderPane();
		// 个人基本信息面板
		GridPane infoPane = new GridPane();
		infoPane.setHgap(50);
		infoPane.setVgap(50);
		infoPane.add(new Label("姓名:"), 0, 0);
		infoPane.add(new Label("陈XX"), 1, 0);
		infoPane.add(new Label("学号:"), 2, 0);
		infoPane.add(new Label("201627010604"), 3, 0);
		infoPane.add(new Label("请选择保险:"), 0, 1);
		ComboBox<String> insuranceType = new ComboBox<String>();
		insuranceType.setValue(InsuranceType.BODY_INJURE_CN);
		insuranceType.getItems().addAll(InsuranceType.BODY_INJURE_CN, InsuranceType.CAR_DESTROY_CN,
				InsuranceType.PERSON_INJURE_CN, InsuranceType.MULTI_ACCIDENT_CN);
		infoPane.add(insuranceType, 1, 1);
		Button showBtn = new Button("显示保险信息");
		infoPane.add(showBtn, 1, 2);
		// 个人照片面板
		Image image = new Image("image/1.jpg");
		ImageView imageView = new ImageView(image);
		imageView.setFitHeight(200);
		imageView.setFitWidth(200);
		// 设置个人面板内容
		personPane.setLeft(imageView);
		personPane.setCenter(infoPane);
		// 保险描述信息内容区域
		TextArea textArea = new TextArea();
		// 设置主面板内容
		mainPane.setCenter(personPane);
		mainPane.setBottom(textArea);
		// 设置stage
		primaryStage.setScene(new Scene(mainPane, 650, 500));
		primaryStage.setTitle("实验二:简单工厂方法");
		primaryStage.show();

		// 点击按钮显示保险信息
		showBtn.setOnAction(e -> {
			String type = insuranceType.getValue();
			showInsuranceDetails(textArea, type);
		});
	}

	/**
	 * 显示保险信息
	 * 
	 * @param textArea
	 * @param type
	 */
	private void showInsuranceDetails(TextArea textArea, String type) {
		InsuranceFactory factory = this.getRealFactory(type);
		Insurance insurance = factory.getInsurance();
		textArea.setText(insurance.getInsuranceInfo());
	}

	/**
	 * 根据选择的中文保险类型，返回英文的保险类型
	 * 
	 * @param type
	 * @return String
	 */
	private InsuranceFactory getRealFactory(String type) {
		InsuranceFactory factory = null;
		if (InsuranceType.BODY_INJURE_CN.equals(type)) {
			factory = new BodyInjureInsuranceFactory();
		}
		if (InsuranceType.CAR_DESTROY_CN.equals(type)) {
			factory = new CarDestroyInsuranceFactory();
		}
		if (InsuranceType.PERSON_INJURE_CN.equals(type)) {
			factory = new PersonInjureInsuranceFactory();
		}
		if (InsuranceType.MULTI_ACCIDENT_CN.equals(type)) {
			factory = new MultiAccidentInsuranceFactory();
		}
		return factory;
	}

	public static void main(String[] args) {
		Application.launch(args);
	}

}
